/*------------------------------------------------------------------------------

Publication: Schakel, W. (2020) Representing the Rich: Economic and Political
	Inequality in Established Democracies. University of Amsterdam: Doctoral
	Dissertation.

Chapter: 4

Description: This do-file runs the analyses presented in the results section of
	chapter 4.

Sections:

	1. Main analyses
	2. Online appendix 4A (robustness checks)
	3. Online appendix 4B (party programs and policy)

Data: Schakel_2020_Chapter4_1_Data (see Schakel_2020_Chapter4_2_Preparation
	do-file for original sources)

	For section 3, the following data sets are used:

	Comparative Manifesto Project version 2019a, downloaded in November 2019 from:
	https://manifesto-project.wzb.eu/datasets

	Comparative Welfare Entitlements Dataset, downloaded in April 2019 from:
	http://cwed2.org/

	ParlGov cabinet and party data, downloaded in June 2018 from:
	http://www.parlgov.org/data/table/view_cabinet/ (Cabinet)
	http://www.parlgov.org/data/table/view_party/ (Party)

------------------------------------------------------------------------------*/

global data "YOUR PATH HERE"

use "$data\Schakel_2020_Chapter4_1_Data.dta", clear

/*------------------------------------------------------------------------------
 1. Main analyses
------------------------------------------------------------------------------*/

* Table 4.1
mixed llegpit1 p10 loggdpt growtht unempt i.topic i.year || country:, vce(rob)
mixed llegpit1 p90 loggdpt growtht unempt i.topic i.year || country:, vce(rob)
mixed llegpit1 p10 p90 loggdpt growtht unempt i.topic i.year || country:, vce(rob)
mixed llegpit1 q1 loggdpt growtht unempt i.topic i.year || country:, vce(rob)
mixed llegpit1 q5 loggdpt growtht unempt i.topic i.year || country:, vce(rob)
mixed llegpit1 q1 q5 loggdpt growtht unempt i.topic i.year || country:, vce(rob)

* Table 4.2
mixed lleftpit1 p10 loggdpt growtht unempt i.topic i.wave || country: if lrightpit1!=., vce(rob)
mixed lleftpit1 p90 loggdpt growtht unempt i.topic i.wave || country: if lrightpit1!=., vce(rob)
mixed lleftpit1 p10 p90 loggdpt growtht unempt i.topic i.wave || country: if lrightpit1!=., vce(rob)
mixed lleftpit1 q1 loggdpt growtht unempt i.topic i.wave || country: if lrightpit1!=., vce(rob)
mixed lleftpit1 q5 loggdpt growtht unempt i.topic i.wave || country: if lrightpit1!=., vce(rob)
mixed lleftpit1 q1 q5 loggdpt growtht unempt i.topic i.wave || country: if lrightpit1!=., vce(rob)

* Table 4.3
mixed lrightpit1 p10 loggdpt growtht unempt i.topic i.wave || country: if lleftpit1!=., vce(rob)
mixed lrightpit1 p90 loggdpt growtht unempt i.topic i.wave || country: if lleftpit1!=., vce(rob)
mixed lrightpit1 p10 p90 loggdpt growtht unempt i.topic i.wave || country: if lleftpit1!=., vce(rob)
mixed lrightpit1 q1 loggdpt growtht unempt i.topic i.wave || country: if lleftpit1!=., vce(rob)
mixed lrightpit1 q5 loggdpt growtht unempt i.topic i.wave || country: if lleftpit1!=., vce(rob)
mixed lrightpit1 q1 q5 loggdpt growtht unempt i.topic i.wave || country: if lleftpit1!=., vce(rob)

* Table 4.4
gen rmp=p90-p10

mixed llegpit1 rmp p50 loggdpt growtht unempt i.topic i.year || country:, vce(rob)
mixed lleftpit1 rmp p50 loggdpt growtht unempt i.topic i.year || country: if lrightpit1!=., vce(rob)
mixed lrightpit1 rmp p50 loggdpt growtht unempt i.topic i.year || country:, vce(rob)

* Summary tables
tab country wave, sum(year) nofreq nost
tabstat llegpit1 lleftpit1 lrightpit1 all p50 p10 p90 q1 q5 loggdpt growtht	///
	unempt if loggdpt!=., stat(mean sd min max n) col(stat) f(%9.2f)

/*------------------------------------------------------------------------------
 2. Online appendix 4A
------------------------------------------------------------------------------*/

* Table 4A.1 (random intercept models of logged party attention, t+1)
mixed llegpit1 p50 loggdpt growtht unempt i.topic i.year || country:, vce(rob)
mixed llegpit1 all loggdpt growtht unempt i.topic i.year || country:, vce(rob)

* Table 4A.2 (random intercept models of logged attention by right-wing parties
* to right-wing voters, t+1)
mixed lrightpit1 p10r loggdpt growtht unempt i.topic i.year || country:, vce(rob)
mixed lrightpit1 p90r loggdpt growtht unempt i.topic i.year || country:, vce(rob)
mixed lrightpit1 p10r p90r loggdpt growtht unempt i.topic i.year || country:, vce(rob)
mixed lrightpit1 q1r loggdpt growtht unempt i.topic i.year || country:, vce(rob)
mixed lrightpit1 q5r loggdpt growtht unempt i.topic i.year || country:, vce(rob)
mixed lrightpit1 q1r q5r loggdpt growtht unempt i.topic i.year || country:, vce(rob)

* Table 4A.3 (random intercept models of logged attention by left-wing parties
* to left-wing voters, t+1)
mixed lleftpit1 p10l loggdpt growtht unempt i.topic i.year || country: if lrightpit1!=. & p10r!=., vce(rob)
mixed lleftpit1 p90l loggdpt growtht unempt i.topic i.year || country: if lrightpit1!=. & p10r!=., vce(rob)
mixed lleftpit1 p10l p90l loggdpt growtht unempt i.topic i.year || country: if lrightpit1!=. & p10r!=., vce(rob)
mixed lleftpit1 q1l loggdpt growtht unempt i.topic i.year || country: if lrightpit1!=. & p10r!=., vce(rob)
mixed lleftpit1 q5l loggdpt growtht unempt i.topic i.year || country: if lrightpit1!=. & p10r!=., vce(rob)
mixed lleftpit1 q1l q5l loggdpt growtht unempt i.topic i.year || country: if lrightpit1!=. & p10r!=., vce(rob)

* Table 4A.4 (random intercept models of logged attention by left-wing and
* right-wing parties to their own voters, t+1, with preference gaps)
gen rmp_left=p90l-p10l
gen rmp_right=p90r-p10r

mixed lleftpit1 rmp_left p50l loggdpt growtht unempt i.topic i.year || country: if lrightpit1!=. & p10r!=., vce(rob)
mixed lrightpit1 rmp_right p50r loggdpt growtht unempt i.topic i.year || country:, vce(rob)

* Table 4A.5 (random intercept models of logged party attention with preference
* gaps (rich-minus-middle))
gen rmm=p90-p50

mixed llegpit1 rmm loggdpt growtht unempt i.topic i.year || country:, vce(rob)
mixed lleftpit1 rmm loggdpt growtht unempt i.topic i.year || country: if lrightpit1!=., vce(rob)
mixed lrightpit1 rmm loggdpt growtht unempt i.topic i.year || country:, vce(rob)

* Table 4A.6 (random intercept models of logged party attention, t+1 (with
* lagged dependent variable, t-2))
mixed llegpit1 p10 llegpitmin2 loggdpt growtht unempt i.topic i.year || country:, vce(rob)
mixed llegpit1 p90 llegpitmin2 loggdpt growtht unempt i.topic i.year || country:, vce(rob)
mixed llegpit1 p10 p90 llegpitmin2 loggdpt growtht unempt i.topic i.year || country:, vce(rob)
mixed llegpit1 q1 llegpitmin2 loggdpt growtht unempt i.topic i.year || country:, vce(rob)
mixed llegpit1 q5 llegpitmin2 loggdpt growtht unempt i.topic i.year || country:, vce(rob)
mixed llegpit1 q1 q5 llegpitmin2 loggdpt growtht unempt i.topic i.year || country:, vce(rob)

* Table 4A.7 (random intercept models of logged party attention, t+1
* (controlling for government spending))
mixed llegpit1 p10 spend loggdpt growtht unempt i.topic i.year || country:, vce(rob)
mixed llegpit1 p90 spend loggdpt growtht unempt i.topic i.year || country:, vce(rob)
mixed llegpit1 p10 p90 spend loggdpt growtht unempt i.topic i.year || country:, vce(rob)
mixed llegpit1 q1 spend loggdpt growtht unempt i.topic i.year || country:, vce(rob)
mixed llegpit1 q5 spend loggdpt growtht unempt i.topic i.year || country:, vce(rob)
mixed llegpit1 q1 q5 spend loggdpt growtht unempt i.topic i.year || country:, vce(rob)

* Table 4A.8 (random intercept models of logged coalition party attention, t+1)
mixed lgovpit1 p10 loggdpt growtht unempt i.topic i.year || country:, vce(rob)
mixed lgovpit1 p90 loggdpt growtht unempt i.topic i.year || country:, vce(rob)
mixed lgovpit1 p10 p90 loggdpt growtht unempt i.topic i.year || country:, vce(rob)
mixed lgovpit1 q1 loggdpt growtht unempt i.topic i.year || country:, vce(rob)
mixed lgovpit1 q5 loggdpt growtht unempt i.topic i.year || country:, vce(rob)
mixed lgovpit1 q1 q5 loggdpt growtht unempt i.topic i.year || country:, vce(rob)

* Table 4A.9 (random intercept models of logged party attention, t+1
* (limited to country-years with an election in t+1))
mixed llegpit1 p10 loggdpt growtht unempt i.topic i.year || country: if elecyear==1, vce(rob)
mixed llegpit1 p90 loggdpt growtht unempt i.topic i.year || country: if elecyear==1, vce(rob)
mixed llegpit1 p10 p90 loggdpt growtht unempt i.topic i.year || country: if elecyear==1, vce(rob)
mixed llegpit1 q1 loggdpt growtht unempt i.topic i.year || country: if elecyear==1, vce(rob)
mixed llegpit1 q5 loggdpt growtht unempt i.topic i.year || country: if elecyear==1, vce(rob)
mixed llegpit1 q1 q5 loggdpt growtht unempt i.topic i.year || country: if elecyear==1, vce(rob)

* Table 4A.10 (random intercept models of logged party attention, next election
* (no interpolation))
mixed llegnite1 p10 llegnit i.topic i.year || country:, vce(rob)
mixed llegnite1 p90 llegnit i.topic i.year || country:, vce(rob)
mixed llegnite1 p10 p90 llegnit i.topic i.year || country:, vce(rob)
mixed llegnite1 q1 llegnit i.topic i.year || country:, vce(rob)
mixed llegnite1 q5 llegnit i.topic i.year || country:, vce(rob)
mixed llegnite1 q1 q5 llegnit i.topic i.year || country:, vce(rob)

* Table 4A.11 (random intercept models of logged party attention, t+1
* (non-weighted dependent variable))
mixed llegnwt1 p10 loggdpt growtht unempt i.topic i.year || country:, vce(rob)
mixed llegnwt1 p90 loggdpt growtht unempt i.topic i.year || country:, vce(rob)
mixed llegnwt1 p10 p90 loggdpt growtht unempt i.topic i.year || country:, vce(rob)
mixed llegnwt1 q1 loggdpt growtht unempt i.topic i.year || country:, vce(rob)
mixed llegnwt1 q5 loggdpt growtht unempt i.topic i.year || country:, vce(rob)
mixed llegnwt1 q1 q5 loggdpt growtht unempt i.topic i.year || country:, vce(rob)

* Table 4A.12 (Random intercept models of logged party attention, t+1
* (controlling for education))
mixed llegpit1 i1e3 loggdpt growtht unempt i.topic i.year || country:, vce(rob)
mixed llegpit1 i5e3 loggdpt growtht unempt i.topic i.year || country:, vce(rob)
mixed llegpit1 i1e3 i5e3 loggdpt growtht unempt i.topic i.year || country:, vce(rob)

* Table 4A.13 (random intercept models of logged party attention, t+1
* (with country-topics as clusters))
mixed llegpit1 p10 loggdpt growtht unempt i.topic i.year || countrytopic:, vce(rob)
mixed llegpit1 p90 loggdpt growtht unempt i.topic i.year || countrytopic:, vce(rob)
mixed llegpit1 p10 p90 loggdpt growtht unempt i.topic i.year || countrytopic:, vce(rob)
mixed llegpit1 q1 loggdpt growtht unempt i.topic i.year || countrytopic:, vce(rob)
mixed llegpit1 q5 loggdpt growtht unempt i.topic i.year || countrytopic:, vce(rob)
mixed llegpit1 q1 q5 loggdpt growtht unempt i.topic i.year || countrytopic:, vce(rob)

* Table 4A.14 (random intercept models of logged party attention, t+1
* (with country-years as clusters))
mixed llegpit1 p10 loggdpt growtht unempt i.topic i.year || countryyear:, vce(rob)
mixed llegpit1 p90 loggdpt growtht unempt i.topic i.year || countryyear:, vce(rob)
mixed llegpit1 p10 p90 loggdpt growtht unempt i.topic i.year || countryyear:, vce(rob)
mixed llegpit1 q1 loggdpt growtht unempt i.topic i.year || countryyear:, vce(rob)
mixed llegpit1 q5 loggdpt growtht unempt i.topic i.year || countryyear:, vce(rob)
mixed llegpit1 q1 q5 loggdpt growtht unempt i.topic i.year || countryyear:, vce(rob)

* Table 4A.15 (random intercept models of logged party attention, t+1
* (with countries and years as clusters))
mixed llegpit1 p10 loggdpt growtht unempt i.topic || country: || year:, vce(rob)
mixed llegpit1 p90 loggdpt growtht unempt i.topic || country: || year:, vce(rob)
mixed llegpit1 p10 p90 loggdpt growtht unempt i.topic || country: || year:, vce(rob)
mixed llegpit1 q1 loggdpt growtht unempt i.topic || country: || year:, vce(rob)
mixed llegpit1 q5 loggdpt growtht unempt i.topic || country: || year:, vce(rob)
mixed llegpit1 q1 q5 loggdpt growtht unempt i.topic || country: || year:, vce(rob)

* Table 4A.16 (linear regression models of logged party attention, t+1
* (with country fixed effects))
reg llegpit1 p10 loggdpt growtht unempt i.topic i.year i.country, vce(rob)
reg llegpit1 p90 loggdpt growtht unempt i.topic i.year i.country, vce(rob)
reg llegpit1 p10 p90 loggdpt growtht unempt i.topic i.year i.country, vce(rob)
reg llegpit1 q1 loggdpt growtht unempt i.topic i.year i.country, vce(rob)
reg llegpit1 q5 loggdpt growtht unempt i.topic i.year i.country, vce(rob)
reg llegpit1 q1 q5 loggdpt growtht unempt i.topic i.year i.country, vce(rob)

* Table 4.A17 (linear regression models of logged party attention, t+1
* (with jackknifed standard errors by country))
reg llegpit1 p10 loggdpt growtht unempt i.topic i.year, vce(jackknife, cluster(country))
reg llegpit1 p90 loggdpt growtht unempt i.topic i.year, vce(jackknife, cluster(country))
reg llegpit1 p10 p90 loggdpt growtht unempt i.topic i.year, vce(jackknife, cluster(country))
reg llegpit1 q1 loggdpt growtht unempt i.topic i.year, vce(jackknife, cluster(country))
reg llegpit1 q5 loggdpt growtht unempt i.topic i.year, vce(jackknife, cluster(country))
reg llegpit1 q1 q5 loggdpt growtht unempt i.topic i.year, vce(jackknife, cluster(country))

/*------------------------------------------------------------------------------
 3. Online appendix 4B
------------------------------------------------------------------------------*/

* First we match the ParlGov cabinet data with the ParlGov party identifiers.
use "$data\ParlGov Parties.dta", clear

keep cmp party_id party_name* country*

tempfile parlgov_cmp
save `parlgov_cmp'

use "$data\ParlGov Cabinet.dta", clear

merge m:1 party_id using `parlgov_cmp'
drop if _merge==2
drop _merge

* We only keep the country-years that can be matched to the CWED.
encode country_name, gen(temp)
keep if inlist(temp,1,2,5,9,11,12,13,14,17,19,20,25,26,27,29,33,34,35,37)
keep if year>1969 & year<2011

rename country_name countryname

keep countryname election_d* cabinet_party seats party_name_e* year cmp party_id

* We keep only one election per country-year. If a party was in the coalition
* following any of these elections, it counts as a coalition party.
collapse (mean) party=cmp seats=seats cabinet=cabinet_party,				///
	by(countryname year party_id party_name_english)
recode cabinet (0.1/0.92=1)

* There is one party that still has multiple values within one country-year;
* we drop the duplicate for this one.
drop if party_id==1246 & year==2006

* We merge the ParlGov data with the CMP using the party identifier.
preserve
	drop if party==.
	tempfile parlgov
	save `parlgov'
restore

* There are also a few cabinet parties where the party identifier is missing, 
* which we merge using the number of seats they obtained. Before doing so, we
* correct the seat number for several parties in the ParlGov data.
keep if party==. & cabinet==1 & seats!=0
replace seats=8 if party_id==2271 & year==1997
replace seats=67 if party_id==840 & year==1996

rename cabinet cabinet2

tempfile parlgov2
save `parlgov2'

* Comparative Manifesto Data.
use "$data\MPDataset_MPDS2019a.dta", clear
gen year=round(date/100,1)
rename absseat seats

* We only keep the country-years that can be matched to the CWED.
keep if inlist(country, 11, 12, 13, 14, 22, 31, 32, 33, 34, 35, 41, 42, 43,	///
	51, 53, 61, 62, 63, 64, 71, 113)
keep if year>1969 & year<2011

merge m:1 countryname year party using `parlgov'
drop if _merge==2
drop _merge

merge m:1 countryname year seats using `parlgov2'
drop if _merge==2
drop _merge

replace cabinet=cabinet2 if cabinet2!=. & cabinet!=cabinet2

gen perseats=seats/totseats
keep countryname year party per504 per505 perseats cabinet

* We manually supplement missing data on cabinet composition for Italy and South
* Korea in 2008 and for the United States between 1970 and 2008. In the US,
* cabinet parties are those that control at least one house of Congress.
replace cabinet=1 if party==32061 & year==2008
replace cabinet=1 if party==113630 & year==2008
replace cabinet=1 if party==61320 & (year<1993 | year==2008)
replace cabinet=1 if party==61620 & (year==1984 | year==1996 | year==2000 |	///
	year==2004)

* The independent variable is calculated for government parties (weighted by
* seat share, the baseline), for government parties (not weighted) and all
* parties in the legislature (weighted by seat share).
preserve
	keep if cabinet==1
	collapse (mean) wpgov=per504 wngov=per505 [pw=perseats], by(countryname year)
	gen welgov=ln(wpgov+0.5)-ln(wngov+0.5)
	gen welgov_nl=wpgov-wngov
	tempfile mpdgov
	save `mpdgov'
restore

preserve
	keep if cabinet==1
	collapse (mean) wpgovnw=per504 wngovnw=per505, by(countryname year)
	gen welgovnw=ln(wpgovnw+0.5)-ln(wngovnw+0.5)
	gen welgovnw_nl=wpgovnw-wngovnw
	tempfile mpdgovnw
	save `mpdgovnw'
restore

preserve
	collapse (mean) wpleg=per504 wnleg=per505 [pw=perseats], by(countryname year)
	gen welleg=ln(wpleg+0.5)-ln(wnleg+0.5)
	gen welleg_nl=wpleg-wnleg
	tempfile mpdleg
	save `mpdleg'
restore

* Comparative Welfare Entitlements Dataset
use "$data\CWED.dta", clear

* We drop countries that have missing values on total generosity and Belgium,
* since Belgium is not in the public opinion data used for the main analysis.
drop if ccode==3 | ccode>22

rename country countryname
keep countryname ccode year totgen uegen skgen pgen
replace countryname="South Korea" if countryname=="Korea"

merge 1:1 countryname year using `mpdgov', nogen
merge 1:1 countryname year using `mpdgovnw', nogen
merge 1:1 countryname year using `mpdleg', nogen

xtset ccode year

gen dgen1=((F.totgen-totgen)/totgen)*100
gen dgen2=((F2.totgen-totgen)/totgen)*100
gen dgen3=((F3.totgen-totgen)/totgen)*100
gen dgenav13=(dgen1+dgen2+dgen3)/3

gen elecyear=1 if welgov!=.
replace elecyear=2 if welgov!=. & F.elecyear==.
replace elecyear=3 if welgov!=. & F2.elecyear==. & elecyear==2
replace elecyear=4 if welgov!=. & F3.elecyear==. & elecyear==3
replace elecyear=5 if welgov!=. & F4.elecyear==. & elecyear==4

label var countryname "Country (string)"
label var ccode "Country (numeric)"
label var year "Year"
label var totgen "Total generosity"
label var uegen "Unemployment generosity"
label var skgen "Sickness generosity"
label var pgen "Pension generosity"
label var wpgov "Positive attention towards the welfare state (government parties)"
label var wngov "Negative attention towards the welfare state (government parties)"
label var welgov "Position towards the welfare state (government parties)"
label var welgov_nl "Position towards the welfare state (government parties, non-logged)"
label var wpgovnw "Positive attention towards the welfare state (government parties, non-weighted)"
label var wngovnw "Negative attention towards the welfare state (government parties, non-weighted)"
label var welgovnw "Position towards the welfare state (government parties, non-weighted)"
label var welgovnw_nl "Position towards the welfare state (government parties, non-weighted, non-logged)"
label var wpleg "Positive attention towards the welfare state (legislature)"
label var wnleg "Negative attention towards the welfare state (legislature)"
label var welleg "Position towards the welfare state (legislature)"
label var welleg_nl "Position towards the welfare state (legislature, non-logged)"
label var dgen1 "Change in total welfare generosity (t - t+1)"
label var dgen2 "Change in total welfare generosity (t - t+2)"
label var dgen3 "Change in total welfare generosity (t - t+3)"
label var dgenav13 "Average change in total welfare generosity (t - t+3)"
label var elecyear "Years until next election"

* Table 4B.1 (effect of party positions on two-year changes in welfare generosity)
reg dgen2 welgov totgen i.ccode year if elecyear>2, vce(rob)

* Table 4B.2 (effect of party positions on changes in welfare generosity)
mixed dgen2 welgov totgen year if elecyear>2 || ccode:, vce(rob)
reg dgen2 welgov totgen i.ccode year if elecyear>2 & dgen2<20, vce(rob)
reg dgen1 welgov totgen i.ccode year if elecyear>1, vce(rob)
reg dgen3 welgov totgen i.ccode year if elecyear>2, vce(rob)
reg dgen2 welgov totgen i.ccode i.year if elecyear>2, vce(rob)
reg dgen2 welgov_nl totgen i.ccode year if elecyear>2, vce(rob)
reg dgen2 welgovnw totgen i.ccode year if elecyear>2, vce(rob)
reg dgen2 welgov totgen i.ccode year, vce(rob)
